﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.Windows.Controls;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media.Animation;
using ZedGraph;
using System.IO;

namespace UI.Comm.Common
{
    public static class Tools
    {

        public static Storyboard PerformOutEffect(DependencyObject target)
        {
            DoubleAnimation animationForOpacity = new DoubleAnimation();
            Storyboard.SetTarget(animationForOpacity, target);
            Storyboard.SetTargetProperty(
                animationForOpacity, new PropertyPath(FrameworkElement.OpacityProperty));
            animationForOpacity.From = 1;
            animationForOpacity.To = 0;
            animationForOpacity.Duration = new Duration(TimeSpan.FromSeconds(0.3));
            animationForOpacity.AutoReverse = false;
            Storyboard storyBoard = new Storyboard();
            
            storyBoard.RepeatBehavior = new RepeatBehavior(1);
            storyBoard.Children.Add(animationForOpacity);

            return storyBoard;
        }
        public static bool CheckTimeSpen(DateTime? startTime, DateTime? endTime,out string message)
        {
            if (startTime == null)
            {
                message="开始时间不能为空!";
                return false;
            }
            else if (endTime == null)
            {
                message = "结束时间不能为空!";
                return false;
            }
            else if (startTime < DateTime.Parse("1800-01-01") || endTime < DateTime.Parse("1800-01-01"))
            {
                message = "时间不能小于1800年1月1日!";
                return false;
            }
            else if (startTime > endTime)
            {
                message = "开始时间不能小于结束时间!";
                return false;
            }
            else
            {
                message = string.Empty;
                return true;
            }
        }
        public static Storyboard PerformInEffect(DependencyObject target)
        {
            DoubleAnimation animationForOpacity = new DoubleAnimation();
            Storyboard.SetTarget(animationForOpacity, target);
            Storyboard.SetTargetProperty(
                animationForOpacity, new PropertyPath(FrameworkElement.OpacityProperty));

            animationForOpacity.From = 0;
            animationForOpacity.To = 1;
            animationForOpacity.Duration = new Duration(TimeSpan.FromSeconds(0.5));
            Storyboard storyBoard = new Storyboard();
            storyBoard.RepeatBehavior = new RepeatBehavior(1);
            storyBoard.Children.Add(animationForOpacity);
            return storyBoard;
        }

        public static void ShowMessage(bool isSuccess, string message)
        {
            DialogParameters parameters = new DialogParameters();
            parameters.OkButtonContent = "确定";
            TextBlock block = new TextBlock();
            block.MinWidth = 100;
            block.MaxWidth = 350;
            block.Text = message;
            block.TextWrapping = TextWrapping.Wrap;
            parameters.Content = block;
            if (isSuccess)
            {
                parameters.Header = "提示消息";
            }
            else
            {
                parameters.Header = "错误提示";
            }
            RadWindow.Alert(parameters);
            //AlertDiaglog d = new AlertDiaglog();
            //d.MessageBlock.Text = message;
            //if (isSuccess)
            //{
            //    d.Title = "提示消息";
            //}
            //else
            //{
            //    d.Title = "错误提示";
            //}
        //    d.ShowInTaskbar = false;
         //   d.ShowDialog();

        }

        public static void OpenConfirm(string message, EventHandler<WindowClosedEventArgs> closedCallblack)
        {
            DialogParameters parameters = new DialogParameters();
            parameters.Theme = new VistaTheme();
            TextBlock block = new TextBlock();
            block.MinWidth = 100;
            block.MaxWidth = 350;
            block.Text = message;
            block.TextWrapping = TextWrapping.Wrap;

            parameters.Content = block;
            parameters.Header = "确认操作";
            parameters.OkButtonContent = "确定";
            parameters.CancelButtonContent = "取消";
            parameters.Closed = closedCallblack;
            RadWindow.Confirm(parameters);
        }
        public static void OpenPrompt(string header, string message, EventHandler<WindowClosedEventArgs> closedCallblack)
        {
            DialogParameters parameters = new DialogParameters();
            TextBlock block = new TextBlock();
            block.MinWidth = 100;
            block.MaxWidth = 350;
            block.Text = message;
            block.TextWrapping = TextWrapping.Wrap;

            parameters.Content = block;
            parameters.Header = header;
            parameters.OkButtonContent = "确定";
            parameters.CancelButtonContent = "取消";
            parameters.Closed = closedCallblack;
            RadWindow.Prompt(parameters);
        }
        public static List<T> CheckSelectItems<T>(object view)
        {
            if (view.GetType().GetProperty("SelectedItem").GetValue(view, null) == null)
            {
                DialogParameters parameters = new DialogParameters();
                //   parameters.Theme = ThemeManager.FromName("Office_Black");
                parameters.Header = "操作提示";
                parameters.Content = "请选中要操作的记录";
                parameters.OkButtonContent = "确定";
                RadWindow.Alert(parameters);
                return null;
            }
            else
            {
                List<T> list = new List<T>();
                foreach (object item in (IList<object>)view.GetType().GetProperty("SelectedItems").GetValue(view, null))
                {
                    list.Add((T)item);
                }
                return list;
            }
        }
        public static RadWindow OpenWindow(string header, FrameworkElement content)
        {
            return OpenWindow(header, content, null, null, null);
        }
        public static RadWindow OpenWindow(string header, FrameworkElement content, string icon)
        {
            return OpenWindow(header, content, icon, null, null);
        }

        public static RadWindow OpenWindow(string header, FrameworkElement content, string icon, double? width, double? height)
        {
            try
            {
                RadWindow window = new RadWindow();
                if (width != null)
                    window.Width = width.Value;

                if (height != null)
                    window.Height = height.Value;
                window.Header = header;
                window.Icon = icon;
                window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                window.Content = content;
                window.Opacity = 1;
                window.Closed += (o, e) =>
                {
                    Tools.PerformOutEffect(window).Begin();
                };
                window.ShowDialog();
                Tools.PerformInEffect(window).Begin();
                return window;
                //CUIWindow window = new CUIWindow();
                //if (width != null)
                //{
                //    window.Width = width.Value;
                //}
                //else
                //{
                //   // window.SizeToContent = SizeToContent.Width;
                //} 
                //if (height != null)
                //{
                //    window.Height = height.Value;
                //}
                //else
                //{
                //  //  window.SizeToContent = SizeToContent.Height;
                //}

                //window.Title = header;
                //window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                //window.Content = content;
                //window.Opacity = 1;
                //return window;
            }
            catch
            {
                return null;
            }
        }

        //将传过来的心电值生成心电图片,把心电图片的路径地址返回
        public static byte[] generateEcgDiagram(int[] datas, int sampleRate)
        {
            ZedGraph.GraphPane myPane = new GraphPane();
            //sample value
            string[] xDatas = new string[datas.Length + 2]; //横坐标在右边多绘制一点出来
            int len = datas.Length; //和总的采样数相等totalsamples
            int paneRectwidth = len + 10;
            myPane.Rect = new System.Drawing.Rectangle(10, 10, paneRectwidth / 2, 200);

            //数据点集合（包含横纵坐标点）
            PointPairList list = new PointPairList();

            for (int i = 0; i < len; i++)
            {
                xDatas[i] = i.ToString();
                //横坐标点
                double x = Convert.ToDouble(i);
                //纵坐标点
                double y = datas[i];
                //添加到集合中
                list.Add(x, y);
            }

            //补足剩余长度的两个点
            xDatas[len] = len.ToString();
            xDatas[len + 1] = (len + 1).ToString();


            //设置横坐标的类型为文本
            myPane.XAxis.Type = AxisType.Text;

            //设置横坐标的开始的最小值
            myPane.XAxis.Scale.Min = 0;
            //设置横坐标的结束的最大值
            myPane.XAxis.Scale.Max = len + 10;
            myPane.YAxis.Scale.Min = 30;
            myPane.YAxis.Scale.Max = 230;
            //设置横坐标要显示的文本
            myPane.XAxis.Scale.TextLabels = xDatas;
            //设置横坐标的字体大小
            myPane.XAxis.Scale.FontSpec.Size = 17;
            //设置纵坐标的字体大小
            myPane.YAxis.Scale.FontSpec.Size = 17;

            myPane.XAxis.Scale.MinorStep = 1;//X轴小步长1,也就是小间隔   
            myPane.XAxis.Scale.MajorStep = sampleRate;//X轴大步长为5，也就是显示文字的大间隔  


            myPane.YAxis.Scale.MinorStep = 10;//X轴小步长1,也就是小间隔   
            myPane.YAxis.Scale.MajorStep = 30;//X轴大步长为5，也就是显示文字的大间隔              
            myPane.YAxis.Scale.IsVisible = true;
            //网格设置
            myPane.YAxis.MajorGrid.DashOff = 1;
            myPane.XAxis.MajorGrid.DashOff = 1;
            myPane.XAxis.MajorGrid.IsVisible = true;
            myPane.YAxis.MajorGrid.IsVisible = true;

            string xCol = "#FF20B2AA";
            string yCol = "#FF20B2AA";

            System.Drawing.Color xC = System.Drawing.ColorTranslator.FromHtml(xCol);
            System.Drawing.Color yC = System.Drawing.ColorTranslator.FromHtml(yCol);

            myPane.XAxis.MajorGrid.Color = xC;
            myPane.YAxis.MajorGrid.Color = yC;


            //将横纵坐标点添加到面板中，并设置名称，值，颜色，点的形状
            string sampleCol = "#FFFF0000";
            System.Drawing.Color sC = System.Drawing.ColorTranslator.FromHtml(sampleCol);
            LineItem curve = myPane.AddCurve("", list, sC, SymbolType.None);

            //设置各点的连线宽度
            //curve.Line.Width = 1.0F;
            curve.Line.IsSmooth = false;
            //设置点的填充颜色
            curve.Symbol.Fill = new Fill(System.Drawing.Color.White);
            curve.Symbol.IsVisible = false;

            //设置连接线的抗锯齿效果
            curve.Line.IsAntiAlias = true;

            //设置图形的填充颜色（渐变效果，及渐变的角度）
            myPane.Chart.Fill = new Fill(System.Drawing.Color.Black);
            myPane.AxisChange();
            string sFileName = ".\\" + Guid.NewGuid().ToString() + ".png";

            MemoryStream stream = new MemoryStream();
            myPane.GetImage().Save(stream, System.Drawing.Imaging.ImageFormat.Png);

            return stream.ToArray();
        }
    
    }
}
